import 'dart:io';

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yp/config/event_bus.dart';
import 'package:yp/config/index.dart';
import 'package:yp/pages/trolley/trolley_logic.dart';

import '../../helper.dart';
import '../../router.dart';
import 'logic.dart';
import 'state.dart';

class MainPage extends StatefulWidget {
  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  final MainLogic logic = Get.put(MainLogic());
  final MainState state = Get.find<MainLogic>().state;
  RxInt cartItemsNum = Get.put(TrolleyLogic()).cartItemsNum;
  // final TrolleyState cardState = Get.find<TrolleyLogic>().state;
  Widget angleMark(String cartItemsNum) {
    return Align(
      alignment: Alignment.topRight,
      child: Container(
        width: cartItemsNum.length >= 3 ? 21 : 18,
        height: cartItemsNum.length >= 3 ? 21 : 18,
        decoration: BoxDecoration(
          color: Color(0xffff322e),
          shape: BoxShape.circle,
        ),
        alignment: Alignment.center,
        child: Text(
          cartItemsNum,
          style: TextStyle(
            color: Color(0xffffffff),
            fontSize: 11,
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Obx(() {
      return Stack(
        children: [
          Scaffold(
            body: PageView(
              physics: NeverScrollableScrollPhysics(),
              controller: state.pageController,
              children: state.pages,
            ),
            bottomNavigationBar: BottomNavigationBar(
              type: BottomNavigationBarType.fixed,
              selectedItemColor: EsColors.Primary,
              unselectedItemColor: Color(0xffcccccc),
              items: [
                BottomNavigationBarItem(
                  icon: Image.asset(
                    'assets/images/mallIcon.png',
                    width: 45.w,
                    height: 45.w,
                  ),
                  activeIcon: Image.asset(
                    'assets/images/mallIconActive.png',
                    width: 45.w,
                    height: 45.w,
                  ),
                  label: '商城',
                ),
                BottomNavigationBarItem(
                  icon: Image.asset(
                    'assets/images/classificationIcon.png',
                    width: 45.w,
                    height: 45.w,
                  ),
                  activeIcon: Image.asset(
                    'assets/images/classificationIconActive.png',
                    width: 45.w,
                    height: 45.w,
                  ),
                  label: '分类',
                ),

                BottomNavigationBarItem(
                  icon: SizedBox(
                    width: 58,
                    child: Stack(
                      children: [
                        Align(
                          alignment: Alignment.center,
                          child: new Image.asset(
                            'assets/images/shoppingCartIcon.png',
                            width: 45.w,
                            height: 45.w,
                          ),
                        ),
                        cartItemsNum != null &&
                                cartItemsNum.value != null &&
                                cartItemsNum.value > 0
                            ? angleMark(cartItemsNum.value.toString())
                            : Container(),
                      ],
                    ),
                  ),
                  activeIcon: SizedBox(
                    width: 58,
                    child: Stack(
                      children: [
                        Align(
                          alignment: Alignment.center,
                          child: new Image.asset(
                            'assets/images/shoppingCartIconActive.png',
                            width: 45.w,
                            height: 45.w,
                          ),
                        ),
                        cartItemsNum != null &&
                                cartItemsNum.value != null &&
                                cartItemsNum.value > 0
                            ? angleMark(cartItemsNum.value.toString())
                            : Container(),
                      ],
                    ),
                  ),
                  label: "购物车",
                ),
                BottomNavigationBarItem(
                  icon: Image.asset(
                    'assets/images/myIcon.png',
                    width: 45.w,
                    height: 45.w,
                  ),
                  activeIcon: Image.asset(
                    'assets/images/myIconActive.png',
                    width: 45.w,
                    height: 45.w,
                  ),
                  label: '我的',
                ),
              ],
              currentIndex: state.selectedIndex.value,
              selectedFontSize: 12,
              unselectedFontSize: 12,
              onTap: (index) => logic.switchTap(index),
              backgroundColor: Colors.white,
            ),
          ),

        ],
      );
    });
  }
}
